package com.miyake.client.datamanage;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.googlecode.gchart.client.GChart;
import com.miyake.client.DataService;
import com.miyake.client.DataServiceAsync;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.OkCancelDialog;
import com.miyake.client.tools.ShowDialog;
import com.miyake.client.tools.SmallButton;

public class ShowChildList {
	private DataServiceAsync service = DataService.Util.getInstance();
	private NodeUpdateHandler nodeUpdateHandler;
	
	public ShowChildList(final NodeProperty node, final VerticalPanel panel, NodeUpdateHandler handler) {
		this.nodeUpdateHandler = handler;
		
		if (node.getChildren().size() == 0) {
			return;
		}
		final VerticalPanel container = new VerticalPanel();
		container.add(new HTML("<b>Contents</b>"));
		container.setBorderWidth(1);
		//container.setWidth("100%");
		panel.add(container);
		final Grid grid = new Grid();
		grid.setBorderWidth(1);
		grid.resize(node.getChildren().size() + 1, 3);
		container.add(grid);
		
		grid.setWidget(0, 0, new HTML("<b>Name</b>"));
		final CheckBox check = new CheckBox("");
		check.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				for (int i = 1; i < grid.getRowCount(); i++) {
					((CheckBox)grid.getWidget(i, 0)).setValue(check.getValue());
				}
			}
		});
		grid.setWidget(0, 0, check);
		
		int row = 1;
		
		for (final NodeProperty child : node.getChildren()) {
			int col = 0;
			grid.setWidget(row, col++, new CheckBox());
			grid.setWidget(row, col++, new ClickableLabel(child.getName(), new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					//fireNodeChangeEvent(child.getKey());
					nodeUpdateHandler.onNodeChange(child.getKey());
				}
				
			}));
			//grid.setWidget(row, col++, new Label(""));
			
			row++;
		}
		
		HorizontalPanel h = new HorizontalPanel();
		h.add(new SmallButton("Delete", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				new ShowDialog() {

					@Override
					protected void onOK(String text) {
						for (NodeProperty n : getSelectedNodes(node, grid)) {
							deleteNode(n.getKey());
						}	
					}

					@Override
					protected DialogBox getDialogBox() {
						return new OkCancelDialog();
					}
				
				};

			}
		}));

		final VerticalPanel subPanel = new VerticalPanel();
		
		h.add(new SmallButton("Show as list", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				subPanel.clear();
				
				for (NodeProperty n : getSelectedNodes(node, grid)) {
					ShowSubItems showSub = new ShowSubItems(n, subPanel, nodeUpdateHandler);
					showSub.setDataNameVisible(true);
					showSub.update();
				}
			}
		}));
		
		//h.add(createOverlayButton(node, grid, subPanel));
		panel.add(subPanel);
		container.add(h);
	}

	protected SmallButton createOverlayButton(final NodeProperty node,
			final Grid grid, final VerticalPanel subPanel) {
		return new SmallButton("Show overlay", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				subPanel.clear();
				final GChart chart = new GChart();
				
				subPanel.add(chart);
				for (NodeProperty n : getSelectedNodes(node, grid)) {
					if (n.getType().equals(NodeProperty.PLOT)) {
						chart.addCurve();
						service.getPlot(n.getKey(), 400, new AsyncCallback<PlotData>() {

							@Override
							public void onFailure(Throwable caught) {

							}

							@Override
							public void onSuccess(PlotData result) {
								for (int i = 0; i < result.getY().length; i++) {
									chart.getCurve(chart.getNCurves()-1).addPoint(i, result.getY()[i]);
								}
							}
							
						});

					}
				}
			}
		});
	}
	
	protected List<NodeProperty> getSelectedNodes(final NodeProperty node, final Grid grid) {
		List<NodeProperty> ret = new ArrayList<NodeProperty>();
		int row = 1;
		for (NodeProperty child : node.getChildren()) {
			if ( ((CheckBox)grid.getWidget(row++, 0)).getValue().equals(true)) {
				ret.add(child);
			}
		}
		
		return ret;
	}
	
	protected void deleteNode(String key) {
		service.deleteNode(key, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(Void result) {
				//fireNodeUpdateEvent("");
				nodeUpdateHandler.onNodeUpdate("");
			}
		
		});
	}
}
